{extend name="public/layout" /}
{block name="body"}
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <table id="admin-table" lay-filter="data-list"></table>
            </div>
        </div>
    </div>
{/block}
{block name="script"}
    <script src="/static/admin/js/action.js?v={$version}"></script>
    <script type="text/html" id="row-handler">
        <a class="layui-btn" lay-event="detail">编辑</a>
        <a class="layui-btn" lay-event="del">删除</a>
    </script>
    <script type="text/html" id="toolbar">
        <a class="layui-btn" lay-event="add">添加</a>
    </script>
    <script>
        layui.use('table', function(){
            var table = layui.table;
            $ = layui.$;
            table.render({
                elem: '#admin-table'
                ,url: '{:url('index')}'
                ,page: true
                ,toolbar: '#toolbar'
                ,defaultToolbar:['filter']
                ,cols: [[
                    {field: 'id', title: 'ID'}
                    ,{field: 'name', title: '文章标题'}
                    ,{field: 'url', title: '文章链接'}
                    ,{field: 'handler', title: '操作',toolbar:'#row-handler'}
                ]]
                ,parseData: function(res){
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.data.total,
                        "data": res.data.data
                    };
                }
                ,response: {
                    statusCode: 10000 //规定成功的状态码，默认：0
                }
            });

            table.on('tool(data-list)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                if(layEvent === 'detail'){ //查看
                    window.location.href = "{:url('detail')}?id="+data.id;
                } else if(layEvent === 'del'){ //删除
                    layer.confirm('确定删除该文章吗？', function(index){
                        var param = new Object();
                        param.id = data.id;
                        del($,'{:url('del')}',param,function(){
                            obj.del();
                            layer.close(index);
                        })
                    });
                }
            });


            table.on('toolbar(data-list)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'add':
                        window.location.href = "{:url('add')}";
                        break;
                };
            });
        });
    </script>
{/block}